import React, { useState, useEffect } from 'react';

//自定义hooks写法：
function useInfo() {
  //获取数据
  const [info, setInfo] = useState([]);

  //传入依赖空数组实现初次加载渲染一次
  useEffect(() => {
    //请求数据
    (async () => {
      let res = await fetch('https://jsonplaceholder.typicode.com/users').then(
        (res) => res.json()
      );

      // console.log(res);
      //[{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

      //缓存数据
      setInfo(res);
    })();
  }, []);

  //返回外界要用的info
  return info;
}

export default useInfo;
